﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Expo.Training.Entity;
using Expo.Training.BRule;
using System.IO;

public partial class Portal_News_AddNews : PageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        InitCecEditor();

        if (!IsPostBack)
        {
            txt_TopExpired.Value = DateTime.Now.AddDays(15).ToString("yyyy-MM-dd");
        }
        string customerCode = this.Customer.CustomerCode.ToLower();
        if (customerCode != "sttc")
        {
            //rbt_o.Visible = false;
            //rbt_c.Visible = false;
        }
    }

    private void InitCecEditor()
    {
        ED_Editor.Show();
    }

    public void DoAdd(object sender, System.EventArgs e)
    {
        #region 添加新闻
        lt_Header.Text = string.Empty;

        string NewsTitle = HttpUtility.HtmlEncode(this.txt_Title.Text.Trim());
        string NewsContent = this.ED_Editor.Content.Trim();
        DateTime topExpiredDate = DateTime.Parse("1900-1-1");

        if (NewsTitle.Length == 0)
        {
            AddEndScript("input error", "$(\"sp_BulletinT_error\").innerText = \"标题不能为空！\";");
            return;
        }
        if (NewsContent.Length == 0)
        {
            AddEndScript("input error", "$(\"sp_new_error\").innerText = \"内容不能为空！\";");
            return;
        }
        try
        { topExpiredDate = DateTime.Parse(txt_TopExpired.Value); }
        catch { }

        News objNews = new News();
        objNews.Title = NewsTitle;
        objNews.NewsContent = NewsContent;
        Guid user = this.Member.MemberID;
        objNews.Creator = user;
        objNews.CustomerID = this.Member.CustomerID;
        string folderName = ResolveClientUrl("~/Data/PortalNews/");
        if (!Directory.Exists(Server.MapPath(folderName)))
        {
            Directory.CreateDirectory(Server.MapPath(folderName));
        }
        string fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff");
        string fileExtension = Path.GetExtension(this.File1.PostedFile.FileName).ToUpper();
        string filePath = Server.MapPath(folderName + fileName + fileExtension);
        if (fileExtension != "" || fileExtension == null)
        {
            if (!(fileExtension == ".JPEG" || fileExtension == ".JPG" || fileExtension == ".PNG" || fileExtension == ".GIF"))
            {
                this.AddEndScript("error", "alert('不支持的图片格式！');");
                return;
            }
            this.File1.PostedFile.SaveAs(filePath);
            CutImage image = new CutImage();
            image.CutImageBySize(filePath, Server.MapPath(folderName + fileName + "_n" + fileExtension), 35, 35);
            image.CutImageBySize(filePath, Server.MapPath(folderName + fileName + "_p" + fileExtension), 403, 250);
            if (File.Exists(filePath))
            {
                File.Delete(filePath);
            }
            objNews.ImageUrl = fileName + "_n" + fileExtension;
            objNews.PortalImageUrl = fileName + "_p" + fileExtension;
        }
        else
        {
            if (this.customer.CustomerCode.ToLower() == "puma")
            {
                objNews.ImageUrl = "";
            }
            else
            {
                objNews.ImageUrl = "Default.gif";
            }
        }

        string addr = "";
        string Xls_path = "";
        string savePath = "Data/PortalNews/";
        string name = "";
        string type = this.File2.PostedFile.FileName.Substring(this.File2.PostedFile.FileName.LastIndexOf(".") + 1).ToLower();
        int FileSize = 0;
        
        Guid docID = Guid.NewGuid();
        objNews.DocID = docID;

        #region 设置新闻类型

        #region 慈溪新闻
        if (this.rbt_y.Checked)
        {
            objNews.Type = 1;
        }
        else if (this.rbt_n.Checked)
        {
            objNews.Type = 11;
            if (type == "pdf")
            {
                name = this.File2.PostedFile.FileName;
                name = DateTime.Now.ToString("yyyyMMddHHmmss") + "." + type;
                string fileName2 = name;//名字
                string path = this.ResolveClientUrl("~/Data/PortalNews/");
                Xls_path = Path.Combine(path, fileName2);//合并路径
                addr = Server.MapPath(Xls_path);//UP路径
                this.File2.PostedFile.SaveAs(addr);//把上传的文档保存到指定的文件夹中
                FileSize = File2.PostedFile.ContentLength;
                savePath = Path.Combine(savePath, fileName2);//获取从Data开始的路径
            }
            else if (type != "")
            {
                AddEndScript("error", "alert('只能上传pdf文件');");
                return;
            }
        }
        else if (this.rbt_p.Checked)
        {
            objNews.Type = 22;
            if (fileExtension == string.Empty)
            {
                AddEndScript("error", "alert('图片新闻必须上传图片！');");
                return;
            }
        }
        else if (this.rbt_t.Checked)
        {
            objNews.Type = 55;
        }
        else
        {
            objNews.Type = 44;
        }
        #endregion

        #region 巴马新闻

        if (this.rbt_y_bm.Checked)
        {
            objNews.Type = 101;//巴马社区快讯
        }
        else if (this.rbt_n_bm.Checked)
        {
            objNews.Type = 102;//巴马最新公告
            if (type == "pdf")
            {
                name = this.File2.PostedFile.FileName;
                name = DateTime.Now.ToString("yyyyMMddHHmmss") + "." + type;
                string fileName2 = name;//名字
                string path = this.ResolveClientUrl("~/Data/PortalNews/");
                Xls_path = Path.Combine(path, fileName2);//合并路径
                addr = Server.MapPath(Xls_path);//UP路径
                this.File2.PostedFile.SaveAs(addr);//把上传的文档保存到指定的文件夹中
                FileSize = File2.PostedFile.ContentLength;
                savePath = Path.Combine(savePath, fileName2);//获取从Data开始的路径
            }
            else if (type != "")
            {
                AddEndScript("error", "alert('只能上传pdf文件');");
                return;
            }
        }
        else if (this.rbt_p_bm.Checked)
        {
            objNews.Type = 103;//巴马图片新闻
            if (fileExtension == string.Empty)
            {
                AddEndScript("error", "alert('图片新闻必须上传图片！');");
                return;
            }
        }
        else if (this.rbt_t.Checked)
        {
            objNews.Type = 104;//巴马时政新闻
        }
        else
        {
            objNews.Type = 44;
        }
        #endregion
        //if (this.rbt_n.Checked)
        //{
        //    objNews.Type = 2;
        //}
        //if (this.rbt_o.Checked)
        //{
        //    objNews.Type = 3;
        //    if (type == "pdf")
        //    {
        //        name = this.File2.PostedFile.FileName;
        //        name = DateTime.Now.ToString("yyyyMMddHHmmss") + "." + type;
        //        string fileName2 =  name;//名字
        //        string path = this.ResolveClientUrl("~/Data/PortalNews/");
        //        Xls_path = Path.Combine(path, fileName2);//合并路径
        //        addr = Server.MapPath(Xls_path);//UP路径
        //        this.File2.PostedFile.SaveAs(addr);//把上传的文档保存到指定的文件夹中
        //        FileSize = File2.PostedFile.ContentLength;
        //        savePath = Path.Combine(savePath, fileName2);//获取从Data开始的路径
        //    }
        //    else if (type != "")
        //    {
        //        AddEndScript("error", "alert('只能上传pdf文件');");
        //        return;
        //    }
        //}
        //if (this.rbt_c.Checked)
        //{
        //    objNews.Type = 4;
        //    if (type == "pdf")
        //    {
        //        name = this.File2.PostedFile.FileName;
        //        name = DateTime.Now.ToString("yyyyMMddHHmmss") + "." + type;
        //        string fileName2 = name;//名字
        //        string path = this.ResolveClientUrl("~/Data/PortalNews/");
        //        Xls_path = Path.Combine(path, fileName2);//合并路径
        //        addr = Server.MapPath(Xls_path);//UP路径
        //        this.File2.PostedFile.SaveAs(addr);//把上传的文档保存到指定的文件夹中
        //        FileSize = File2.PostedFile.ContentLength;
        //        savePath = Path.Combine(savePath, fileName2);//获取从Data开始的路径
        //    }
        //    else if (type != "")
        //    {
        //        AddEndScript("error", "alert('只能上传pdf文件');");
        //        return;
        //    }
        //}
        #endregion

        objNews.PDFName = name;
        if (this.rbt_m.Checked)
        {
            objNews.Status = 1;
        }
        if (this.rbt_w.Checked)
        {
            objNews.Status = 0;
        }
        if (this.rbt_Yes.Checked)
        {
            objNews.IsTop = 1;
            objNews.TopExpiredDate = topExpiredDate;
        }
        if (this.rbt_No.Checked)
        {
            objNews.IsTop = 0;
        }
        objNews.Tags = txt_tag.Text;
        if (objNews.Add())
        {
            if (name != "")
            {
                Doc_Info objDoc = new Doc_Info();
                objDoc.DocID = docID;
                objDoc.DocName = name;
                objDoc.DocURL = savePath;
                objDoc.DocType = type;
                objDoc.DocSize = FileSize;
                objDoc.CreatorID = this.Member.MemberID;
                objDoc.CustomerID = this.Member.CustomerID;
                objDoc.Add();
                objDoc.Dispose();
            }
            Expo.Training.BRule.Audit.Audit.AuditLog(this.Member.MemberID, "新闻管理 - 添加 - ID:" + objNews.NID);
            Response.Redirect("Default.aspx");
        }
        objNews.Dispose();
        #endregion



    }
}
